﻿namespace EFS.RC.CC.CH.Tables
{
	public static class SurveySessions
	{
		/// Returns an object from a data row
		public static EFS.RC.CC.CH.Entities.Surveys.Sessions.Session FromTableRow(System.Data.DataRow row)
		{
			// check
			EFS.Common.Verify.Reference(row, "row");

			// create new
			EFS.RC.CC.CH.Entities.Surveys.Sessions.Session dataObj = new EFS.RC.CC.CH.Entities.Surveys.Sessions.Session();

			// ID
			dataObj.ID = EFS.DB.DataRow.Int32(row, "ID");

			// OCCURRED
			dataObj.Occurred = EFS.DB.DataRow.DateTime(row, "OCCURRED");

			// SURVEY_ID
			dataObj.SurveyId = EFS.DB.DataRow.Int32(row, "SURVEY_ID");

			// CONVERSATION_GUID
			dataObj.ConversationGuid = EFS.DB.DataRow.Guid(row, "CONVERSATION_GUID");

			// DOCUMENT_ID
			dataObj.DocumentId = EFS.DB.DataRow.String(row, "DOCUMENT_ID");
			
			//SURVEY_STOP
			dataObj.Survey_Stop = EFS.DB.DataRow.String(row, "SURVEY_STOP");
			
			//SURVEY_START
			dataObj.Survey_Start = EFS.DB.DataRow.String(row, "SURVEY_START");

			//SURVEY_CONTINUE
			dataObj.Survey_Continue = EFS.DB.DataRow.String(row, "SURVEY_CONTINUE");

			// ok
			return dataObj;
		}

		/// Create
		public static System.Int32? Create(
			System.Int32 surveyId,
			System.Guid? conversationGuid,
			System.String documentId,
			System.String survey_stop,
			System.String survey_start,
			System.String survey_continue,
			System.String survey_by,
			System.Int32 isCompleted,
			EFS.DB.Connection connection)
		{
			// make params
			EFS.DB.Parameters.Array parametersArray =
				new EFS.DB.Parameters.Array(
					new System.String[] { "survey_id", "conversation_guid", "document_id", "survey_stop", "survey_start", "survey_continue", "survey_by", "is_completed", "id" },
					new System.Object[] { surveyId, conversationGuid, documentId, survey_stop, survey_start, survey_continue, survey_by, isCompleted, default(System.Int32) },
					new System.Boolean[] { false, false, false, false, false, false, false, false, true });

			// exec create
			connection.DoExecuteNonQuery("[dbo].[SurveySessions_Create]", parametersArray);

			// get id value
			return parametersArray.GetOutPrameterValue<System.Int32?>("id"); 
		}

		/// Create
		public static void Update(
			System.Int32 sessionId,
			System.String survey_stop,
			System.String survey_start,
			System.String survey_continue,
			System.Int32 isCompleted,
			EFS.DB.Connection connection)
		{
			// make params
			EFS.DB.Parameters.Array parametersArray =
				new EFS.DB.Parameters.Array(
					new System.String[] { "id", "survey_stop", "survey_start", "survey_continue", "is_completed" },
					new System.Object[] { sessionId, survey_stop, survey_start, survey_continue, isCompleted },
					new System.Boolean[] { false, false, false, false, false });

			// exec create
			connection.DoExecuteNonQuery("[dbo].[SurveySessions_Update]", parametersArray);

		}

		/// Create
		public static System.Int32 GetCountAgainstConversation(
			System.Int32 surveyId,
			System.Guid conversationGuid)
		{
			// make params
			EFS.DB.Parameters.Array parametersArray =
				new EFS.DB.Parameters.Array(
					new System.String[] { "survey_id", "conversation_guid" },
					new System.Object[] { surveyId, conversationGuid },
					new System.Boolean[] { false, false });

			// handle connection
			using (EFS.DB.Connection connection = EFS.RC.CC.CH.QRDConnection.New)
			{
				// exec create
				using (System.Data.DataSet resultDataSet = connection.DoExecuteQuery("[dbo].[SurveySessions_GetCountAgainstConversation]", parametersArray))
				{
					if (resultDataSet.Tables.Count > 0)
					{
						// get tables
						using (System.Data.DataTable table = resultDataSet.Tables[0])
						{
							if (table.Rows.Count > 0)
							{
								return EFS.DB.DataRow.Int32(table.Rows[0], "CNT").Value;
							}
						}
					}
				}

				// none
				return 0;
			}
		}


		/// Create
		public static System.Int32 GetCountAgainstDocument(
			System.Int32 surveyId,
			System.String documentId)
		{
			// make params
			EFS.DB.Parameters.Array parametersArray =
				new EFS.DB.Parameters.Array(
					new System.String[] { "survey_id", "document_id" },
					new System.Object[] { surveyId, documentId },
					new System.Boolean[] { false, false });

			// handle connection
			using (EFS.DB.Connection connection = EFS.RC.CC.CH.QRDConnection.New)
			{
				// exec create
				using (System.Data.DataSet resultDataSet = connection.DoExecuteQuery("[dbo].[SurveySessions_GetCountAgainstDocument]", parametersArray))
				{
					if (resultDataSet.Tables.Count > 0)
					{
						// get tables
						using (System.Data.DataTable table = resultDataSet.Tables[0])
						{
							if (table.Rows.Count > 0)
							{
								return EFS.DB.DataRow.Int32(table.Rows[0], "CNT").Value;
							}
						}
					}
				}

				// none
				return 0;
			}
		}
	}

}